﻿using Infrastructure.Access;
using Infrastructure.Log;
using Infrastructure.Operator;
using Infrastructure.SorketOperate;
using Infrastructure.Web;
using MyApplication;
using MyData.Dto;
using MyData.Models;
using MyWeb.App_Start;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace MyWeb.Controllers
{
    public class HomeController : BaseController
    {

        public ViewResult Login()
        {
            OperatorProvider.Provider.RemoveCurrent();
            return View();
        }
        [HandlerLogin]
        public ViewResult Index()
        {
            return View();
        }
        [HandlerLogin]
        public ViewResult Left()
        {
            return View();
        }
        [HandlerLogin]
        public ViewResult Top()
        {
            return View();
        }
        [HandlerLogin]
        public ViewResult Main()
        {
            return View();
        }
        [HandlerLogin]
        public ViewResult NewView()
        {
            return View();
        }
        [HttpPost]
        public JsonResult LoginAjax(string UserName, string Password, bool IsRemeber)
        {
            var result = new AjaxResult<AdminUser>();
            return base.Try<AdminUser>(() =>
            {
                AdminUser dto = adminUserApp.Login(UserName, Password);
                if (dto == null)
                {
                    throw new KnownException("账号密码错误");
                }
                OperatorModel model = new OperatorModel();
                model.ID = dto.Id;
                model.NickName = dto.NickName;
                model.UserName = dto.UserName;
                model.PassWord = dto.PassWord;
                model.Menus = dto.Menus;
                model.DepartId = dto.DepartId;
                model.LastLoginStr = dto.LastLoginTime.HasValue ? dto.LastLoginTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : "第一次登陆";
                OperatorProvider.Provider.AddCurrent(model);
                adminUserApp.UpdateLastLoginTime(dto.Id);
                if (IsRemeber)
                {
                    WebHelper.WriteCookie("UserName", UserName);
                    WebHelper.WriteCookie("Password", Password);
                }
                else
                {
                    WebHelper.RemoveCookie("UserName");
                    WebHelper.RemoveCookie("Password");
                }
            }, result);


        }

        public ViewResult Video()
        {
            return View();
        }
        public ViewResult LoginNew()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Carserver()
        {
            var input_stream = Request.InputStream;

            // 获取推送数据的长度
            var stream_len = Convert.ToInt32(input_stream.Length);
            string file_path = @"d:\lpr";
            if (!Directory.Exists(file_path))
            {
                Directory.CreateDirectory(file_path);
            }

            // 写入到文件中
            string result_path = @"d:\lpr\plate_result.txt";
            WriteTxt(result_path, stream_len.ToString());

            var stream_buf = new byte[stream_len];
            input_stream.Read(stream_buf, 0, stream_len);

            // 将数据转换成utf-8
            var request_msg = Encoding.UTF8.GetString(stream_buf);
            WriteTxt(result_path, request_msg);
            string plate = ParsePlateResult(request_msg);
            if (plate != OldPlate)
            {
                OldPlate = plate;
                SorketClient.SendSql("0," + plate);
            }
           
            string response_data = "{\"Response_AlarmInfoPlate\":{\"info\":\"\",\"content\":\"...\",\"is_pay\":\"true\"}}";
            Response.Write(response_data);

            Response.End();
            return Json(null,JsonRequestBehavior.AllowGet);

        }
        static string OldPlate = "";
        [HttpPost]
        public JsonResult Carserver1()
        {
            var input_stream = Request.InputStream;

            // 获取推送数据的长度
            var stream_len = Convert.ToInt32(input_stream.Length);
            string file_path = @"d:\lpr";
            if (!Directory.Exists(file_path))
            {
                Directory.CreateDirectory(file_path);
            }

            // 写入到文件中
            string result_path = @"d:\lpr\plate_result1.txt";
            WriteTxt(result_path, stream_len.ToString());

            var stream_buf = new byte[stream_len];
            input_stream.Read(stream_buf, 0, stream_len);

            // 将数据转换成utf-8
            var request_msg = Encoding.UTF8.GetString(stream_buf);
            WriteTxt(result_path, request_msg);
            string plate = ParsePlateResult(request_msg);
            if (plate != OldPlate)
            {
                OldPlate = plate;
                SorketClient.SendSql("1," + plate);
            }
           
            string response_data = "{\"Response_AlarmInfoPlate\":{\"info\":\"\",\"content\":\"...\",\"is_pay\":\"true\"}}";
            Response.Write(response_data);

            Response.End();
            return Json(null, JsonRequestBehavior.AllowGet);

        }
        public string ParsePlateResult(string push_result)
        {
            try
            {
                string[] array = push_result.Split('&');
                string[] plate = array[1].Split('=');
                return plate[1];
            }
            catch (Exception)
            {
                return "";
            }
        }
        public void WriteTxt(string path, string text)
        {
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs, Encoding.Default);
            sw.Write(text);
            sw.Close();
            fs.Close();
        }

        

    }
}